package com.shunhe.zs_pda.ui.fragment

import android.content.DialogInterface
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.ruitu.arad.Arad
import com.ruitu.arad.api.okhttp.Url
import com.ruitu.arad.api.okhttp.response.JsonResponseHandler
import com.ruitu.arad.base.BaseFragment
import com.ruitu.arad.base.BaseModel
import com.ruitu.arad.base.BasePresenter
import com.ruitu.arad.util.ToastUtils
import com.ruitu.arad.util.UIUtils
import com.ruitu.router_module.Logg
import com.ruitu.router_module.bean.EventModel
import com.ruitu.router_module.util.GlideUtil
import com.shunhe.zs_pda.R
import com.shunhe.zs_pda.base.Gl
import com.shunhe.zs_pda.ui.sys.LoginActivity
import com.shunhe.zs_pda.util.find
import com.shunhe.zs_pda.util.toast
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.json.JSONObject

// 个人中心
class MineFragment : BaseFragment<BasePresenter<*, *>, BaseModel>() {
    var rl_0_1: RelativeLayout? = null
    var rl_1: RelativeLayout? = null
    var rl_setting: RelativeLayout? = null
    var rl_kehu: RelativeLayout? = null
    var rl_news: RelativeLayout? = null
    var tv_name: TextView? = null
    var tv_ok: TextView? = null
    var iv_header: ImageView? = null
    var rl_top: RelativeLayout? = null// 顶部
    private var mParam1: String? = null

    companion object {
        private const val ARG_PARAM1 = "param1"
        fun newInstance(param1: String?): MineFragment {
            val fragment = MineFragment()
            val args = Bundle()
            args.putString(ARG_PARAM1, param1)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            mParam1 = requireArguments().getString(ARG_PARAM1)
        }
        Arad.bus.register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onGetBusMsg(ev: EventModel<*>) {
        if (ev.eventCode == 22110951L) { // 获取用户信息完成
            if (null != Gl.user) {
                tv_name!!.text = Gl.user!!.nickname
                GlideUtil.loadImg(Gl.user!!.avatar, iv_header)
            } else {
                tv_name!!.text = "尚未登录"
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        val v = inflater.inflate(R.layout.fragment_mine, container, false)
        rl_0_1 = v.find(R.id.rl_0_1)
        rl_1 = v.find(R.id.rl_1)
        rl_setting = v.find(R.id.rl_setting)
        tv_name = v.find(R.id.tv_name)
        tv_ok = v.find(R.id.tv_ok)
        iv_header = v.find(R.id.iv_header)
        rl_top = v.find(R.id.rl_top)
        rl_news = v.find(R.id.rl_news)
        rl_kehu = v.find(R.id.rl_kehu)
        setListeners()
        return v
    }

    private fun setListeners() {
        setOnClickListener(rl_0_1, rl_1, tv_ok, rl_top, rl_setting, rl_kehu, rl_news)
    }

    override fun onClick(v: View) {
        if (v === rl_0_1) { // 线路列表
            "开发中".toast()
        }
        if (v === rl_top) {
            "开发中".toast()
        }
        if (v === rl_1) { //对账分析
            "开发中".toast()
        }
        if (v === rl_news) { //信息发布
            "开发中".toast()
        }
        if (v === rl_kehu) { //客户分析
            "开发中".toast()
        }
        if (v === tv_ok) {
            UIUtils.showAlertDialog(
                childFragmentManager, "提示", "是否退出登录?",
                "确定", "取消", { _: DialogInterface?, _: Int ->
                    // 调用退出登录接口
                    reqLogout()
                    // 退出登录,删除极光别名
//                    JPushInterface.deleteAlias(Arad.app, 1)
                    Arad.preferences.putString("token", "").flush()
                    Gl.user = null
                    tv_name!!.text = "尚未登录"
                    startActivity(LoginActivity::class.java)
                }, null
            )
        }
        if (v === rl_setting) {
            "开发中".toast()
        }
    }

    // 接口:退出登录
    private fun reqLogout() {
        val token = Arad.preferences.getString("token")
        val params: MutableMap<String, String> = HashMap()
        params["token"] = token
        hideProgress()
        showProgress()
        Gl.okHttp!!.post().url(Url.LOGOUT).params(params)
            .tag(this).enqueue(activity, object : JsonResponseHandler() {
                override fun onFailure(statusCode: Int, error_msg: String) {
                    hideProgress()
                    ToastUtils.showShort("退出登录失败")
                    Logg.i("reqLogout.error_msg = $error_msg")
                }

                override fun onSuccess(statusCode: Int, response: JSONObject) {
                    hideProgress()
                    val code = response.optString("code")
                    val msg = response.optString("msg")
                    val data = response.optString("data")
                    Logg.i("reqLogout.code = $code msg = $msg")
                    Logg.i("reqLogout.data = $data")
                    if (TextUtils.equals("1", code)) {
                    } else {
                    }
                    ToastUtils.showShort(msg)
                }
            })
    }
}